package com.lx.bbs.dao;

import com.lx.bbs.entity.Board;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * BBS论坛-板块表-13 Mapper 接口
 * </p>
 *
 * @author lx
 * @since 2024-11-23
 */
public interface BoardMapper extends BaseMapper<Board> {

    @Select(" select a.*, topic_cnt, c.publishtime `maxTopic.pubilshtime`, c.title `maxTopic.title`, c.cname `maxTopic.user.cname`" +
            " from tbl_board a" +
            " left join (select boardid, count(*) topic_cnt from tbl_topic group by boardid) b on a.id=b.boardid" +
            " left join (select a.publishtime, a.title, b.cname, a.boardid from tbl_topic a join tbl_user b on a.uid=b.id where a.id in (select max(id) maxid from tbl_topic)  ) c on a.id=c.boardid" +
            " where a.parentid = #{pid}")
    // @Results => <ResultMap>
    @Results(
            id = "rm1",
            // @Many 一对多  fetchType 设置加载方式 默认积极加载
            value = {
                    @Result(column = "id", property = "id", id = true),
                    @Result(column = "parentid", property = "parentid"),
                    @Result(column = "id", property = "children",
                    many = @Many(select = "selectByPid"))}
    )
    public List<Board> selectByPid(String pid);

}
